#include "groupsessiondetailwidget.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
    : QDialog(parent)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(300,400);
    this->setWindowTitle("群聊详情");
    this->setWindowIcon(QIcon(":/image/logo.png"));
   // this->setStyleSheet("QWidget{background-color:rgb(245, 245, 245);}");

    //创建垂直布局管理器
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(20,20,20,20);
    layout->setSpacing(10);
    this->setLayout(layout);

    //创建滚动区域
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width: 2px; background-color: rgb(255, 255, 255); }QScrollBar::handle:vertical {background-color: rgb(205, 205, 205);}");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0px;}");

    //创建滚动区域内置QWidget
    container = new QWidget();
    scrollArea->setWidget(container);

    //创建QWidget内置网格布局管理器
    QGridLayout* glayout = new QGridLayout();
    glayout->setContentsMargins(0,0,0,0);
    glayout->setSpacing(0);
    container->setLayout(glayout);

    //群聊Tag
    QLabel* groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel{font-weight:700;}");
    groupNameTag->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

    //添加水平布局管理器
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0,0,0,0);

    //群聊名称
    groupNameLabel = new QLabel();
    groupNameLabel->setText("人类行为研究观察小组");
    groupNameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    groupNameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    //群聊名称更改按钮
    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(20,20);
    modifyBtn->setIconSize(QSize(20,20));
    modifyBtn->setIcon(QIcon(":/image/modify.png"));
    modifyBtn->setStyleSheet("QPushButton {border:none; }");

    hlayout->addWidget(groupNameLabel,0,Qt::AlignLeft);
    hlayout->addWidget(modifyBtn,0,Qt::AlignRight);

    //添加退群按钮
    QPushButton* exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedHeight(50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    exitGroupBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90,90); border-radius: 5px; } QPushButton:pressed { background-color: rgb(235,235, 235); }");

    //整体添加到垂直布局管理器中
    layout->addWidget(scrollArea);
    layout->addWidget(groupNameTag);
    layout->addLayout(hlayout);
    layout->addWidget(exitGroupBtn);

#if test
    for(int i = 0; i < 20; i++)
    {
        AvatarItem* item = new AvatarItem(QIcon(":/image/modify.png"),"测试群聊会话" + QString::number(i));
        this->addMember(item);
    }
#endif
}

void GroupSessionDetailWidget::addMember(AvatarItem* avatarItem)
{
    //获取网格布局管理器
    QGridLayout* layout = dynamic_cast<QGridLayout*>(container->layout());

    //准备添加元素. 添加元素前判断将添加位置是否合法, 不合法则进行换行
    if(col >= MAX_COL)
    {
        ++row, col = 0;
    }
    //添加元素到对应位置上
    layout->addWidget(avatarItem,row,col);
    ++col;
}

void GroupSessionDetailWidget::initData()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(dataCenter,&model::DataCenter::getMemberListDone,this,&GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initMembers(const QString &chatSessionId)
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    auto list = dataCenter->getMemberList(chatSessionId);

    if(list == nullptr)
    {
        LOG() << "GroupSessionDetailWidget::initMembers 失败, list列表为空指针";
        return;
    }

    for(auto& f : *list)
    {
        AvatarItem* item = new AvatarItem(f.userIcon,f.userName);
        addMember(item);
    }

    groupNameLabel->setText("新的群聊");

}
